﻿using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Utilities.Tests
{
    [TestClass()]
    public class ReflectionOnlyTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void ReflectionOnlyIsAttributeDefinedTest()
        {
            Assembly assembly = Assembly.ReflectionOnlyLoadFrom(@"..\..\..\..\TestBinaries.Solution\TransientObject\bin\Debug\TransientObject.dll");

            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler((sender, args) => { return Assembly.ReflectionOnlyLoad(args.Name); });

            Type type = assembly.GetType("TransientObject.TransientObject");
            Assert.IsTrue(ReflectionOnly.IsAttributeDefined(type, "Transient"));

            PropertyInfo property = type.GetProperty("TransientProperty");
            Assert.IsTrue(ReflectionOnly.IsAttributeDefined(property, "Transient"));

            type = assembly.GetType("TransientObject.PersistentObject");
            Assert.IsFalse(ReflectionOnly.IsAttributeDefined(type, "Transient"));

            property = type.GetProperty("PersistentProperty");
            Assert.IsFalse(ReflectionOnly.IsAttributeDefined(property, "Transient"));
        }

        [TestMethod()]
        public void ReflectionOnlyGetAttributeValueTest()
        {
            Assembly assembly = Assembly.ReflectionOnlyLoadFrom(@"..\..\..\..\TestBinaries.Solution\TeachersStudents\bin\Debug\TeachersStudents.dll");

            AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler((sender, args) => { return Assembly.ReflectionOnlyLoad(args.Name); });

            Type type = assembly.GetType("TeachersStudents.Teacher");
            Assert.IsFalse(ReflectionOnly.IsAttributeDefined(type, "Transient"));

            PropertyInfo property = type.GetProperty("TeacherName");
            int length = (int)ReflectionOnly.GetAttributeValue(property, "Length", null);
            Assert.AreEqual(20, length);

            property = type.GetProperty("Teaches");

            Type declaring = ReflectionOnly.GetAttributeValue<Type>(property, "Inverse", "Declaring");
            Assert.AreEqual(assembly.GetType("TeachersStudents.Student"), declaring);

            string propertyName = ReflectionOnly.GetAttributeValue<string>(property, "Inverse", "PropertyName");
            Assert.AreEqual("TaughtBy", propertyName);

            string nonExistent = ReflectionOnly.GetAttributeValue<string>(property, "Inverse", "NonExistent");
            Assert.IsNull(nonExistent);
        }

        [TestMethod]
        public void ReflectionOnlyGetEnumerationItemsTest()
        {
            Assembly assembly = Assembly.ReflectionOnlyLoadFrom(@"..\..\..\..\TestBinaries.Solution\Enumerations\bin\Debug\Enumerations.dll");
            Type enumeration = assembly.GetType("Enumerations.Pets");

            IList<Tuple<string, object>> items = ReflectionOnly.GetEnumerationItems(enumeration);

            Assert.AreEqual(8, items.Count);

            Assert.AreEqual("None", items[0].Item1);
            Assert.AreEqual(0, items[0].Item2);

            Assert.AreEqual("Rodent", items[3].Item1);
            Assert.AreEqual(4, items[3].Item2);

            Assert.AreEqual("Other", items[7].Item1);
            Assert.AreEqual(64, items[7].Item2);
        }
    }
}
